package com.wangwei.basic;

import edu.princeton.cs.algs4.StdOut;

import java.util.Iterator;

public class ResizingArrayStack<Item> implements Iterable<Item>{

    private Item[] a = (Item[]) new Object[1];
    private int N =0;
    public boolean isEmpty(){
        return N == 0;
    }
    public int size(){
        return N;
    }

    private void resize(int max){
        Item[] temp =(Item[]) new Object[max];
        for(int i =0; i<N ;i++){
            temp[i]=a[i];
        }
        a = temp ;
    }

    public void push(Item item){
        if(N == a.length ) resize(a.length * 2);
        a[N++] = item ;
    }

    public Item pop(){
        Item item = a[--N];
        a[N]=null;
        if(N>0 && N == a.length/4) resize(a.length/2);
        return item;
    }


    @Override
    public Iterator<Item> iterator() {
        return new ReverseArrayIterator();
    }

    private class ReverseArrayIterator implements Iterator<Item>{

        //支持先进后出的迭代
        private int i = N ;
        public boolean hasNext(){
            return i >0 ;
        }

        public Item next(){
            return a[--i];
        }

        public void remove(){

        }
    }

    public static void main(String[] args) {
        ResizingArrayStack<Integer> s =new ResizingArrayStack();
        s.push(1);
        s.push(2);
        s.push(3);
        Iterator iterator = s.iterator();

        while(iterator.hasNext()){
            StdOut.println(iterator.next());
        }
    }
}
